import type { AxiosResponse } from 'axios';

// import axios from 'axios';
import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  export interface RegisterParams {
    username: string;
    password: string;
    uuid: string;
    code: string;
  }
  export interface RegisterResult {
    msg: string;
    code: number;
  }
  export interface CaptchaImageResult {
    msg: string;
    img: string;
    code: number;
    captchaOnOff: boolean;
    uuid: string;
  }

  // /** 注册接口参数 */
  // export interface RegisterParams {
  //   username: string;
  //   password: string;
  //   uuid: string;
  //   code: string;
  // }

  /** 登录接口参数 */
  export interface LoginParams {
    password: string;
    username: string;
    uuid: string;
    code: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

// 获取验证码
export const getCaptchaImage = () =>
  baseRequestClient.get<AxiosResponse<AuthApi.CaptchaImageResult>>(
    '/captchaImage',
  );
// 用户注册
export const userRegister = (data: AuthApi.RegisterParams) =>
  baseRequestClient.post<AxiosResponse<AuthApi.RegisterResult>>(
    '/register',
    data,
  );

export function userLogin(data: AuthApi.LoginParams) {
  return requestClient.post('/login', data);
}

export function userLogout() {
  return requestClient.post('/logout');
}

/**
 * 登录
 */
// export async function loginApi(data: AuthApi.LoginParams) {
//   return requestClient.post<AuthApi.LoginResult>('/auth/login', data);
// }

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}

export async function resetPWD(data: AuthApi.LoginParams) {
  return baseRequestClient.put<AxiosResponse<AuthApi.RegisterResult>>(
    '/resetPwd',
    data,
  );
}
